function completeAnalysis()

rootDir='/Users/matthew/workspace/data/mImaginery/eegmmidb/';
outDir='/Users/matthew/workspace/data/mImaginery/data/new/';

filter='S*'
subjNames=dir(fullfile(rootDir,filter));

task1={'R03','R07','R11'};
task2={'R04','R08','R12'};
eventLabelTask1={'_Rleft','_Rright'};
eventLabelTask2={'_Ileft','_Iright'};

for i=1:length(subjNames)
   extractData(rootDir,outDir,subjNames(i).name,task1,eventLabelTask1)
   extractData(rootDir,outDir,subjNames(i).name,task2,eventLabelTask2)
end


function extractData(rootDir,outDir,subjName,task,eventLabels)

fileNames=fileNames2Merge(subjName,task);
extractTaskTrials(rootDir,subjName,fileNames,outDir,eventLabels);

function extractTaskTrials(rootDir,subjName,fileNames,outDir,eventLabels)

for i=1:length(fileNames)
    filename=fullfile(strcat(rootDir,subjName),fileNames{i});
    edfData(i)=readEDFplus(filename);
end

for j=1:length(edfData)
    [dataSx(j),dataDx(j)]=defineTrialsBA(edfData(j));
end    
saveTrial(dataSx,subjName,eventLabels{1},outDir);
saveTrial(dataDx,subjName,eventLabels{2},outDir);

function saveTrial(dataV,subjName,eventLabel,outDir)
data=mergeTrials(dataV);
outfile=strcat(subjName,eventLabel);
save(fullfile(outDir,outfile),'data')   



function [fileNames]=fileNames2Merge(subjName,runNames)

fileNames=strcat(strcat(subjName,runNames),'.edf');

function [data]=mergeTrials(toMerge)
data=toMerge(1);
for i=2:length(toMerge)
    data=merge2Trials(data,toMerge(i));
end

function [dataout]=merge2Trials(data1,data2)

%check same labels and fsample
if(all(strcmp(data1.label,data2.label)) && data1.fsample==data2.fsample)
    dataout.label=data1.label;
    dataout.fsample=data1.fsample;
    dataout.trial=cat(2,data1.trial,data2.trial);
    dataout.time=cat(2,data1.time,data2.time);
    
    dataout.trialRest=cat(2,data1.trialRest,data2.trialRest);
    dataout.timeRest=cat(2,data1.timeRest,data2.timeRest);
    
else
    error('Channels or sample rate between data1 and data2 are different')
end






function [trialsSx,trialsDx]=defineTrialsBA(data)

rest=2;
left=3;
right=4;

trialsSx.fsample=data.fsample;
trialsDx.fsample=data.fsample;
trialsSx.label=data.label;
trialsDx.label=data.label;

rest_indexes=find([data.events(:).type]==rest);
left_indexes=find([data.events(:).type]==left);
right_indexes=find([data.events(:).type]==right);

left_ind=1;
right_ind=1;

r=rest_indexes(1);
s=left_indexes(left_ind);
d=right_indexes(right_ind);
[res,indx_min]= min([s-r d-r]);

if(res<0 && indx_min==1)
   left_indexes=left_indexes(2:end);
else
   right_indexes=right_indexes(2:end);
end

for i=1:length(rest_indexes)

r=rest_indexes(i);

s=left_indexes(min(left_ind,length(left_indexes)));
d=right_indexes(min(right_ind,length(right_indexes)));

[res,indx_min]= min([s-r d-r]);
    
if(res<0 || indx_min==1)
   
   [trialsSx.trialRest{left_ind},trialsSx.timeRest{left_ind}]=extractTrial(data,r);
   [trialsSx.trial{ left_ind},trialsSx.time{left_ind}]=extractTrial(data,s);
    left_ind=left_ind+1;
else
    
    [trialsDx.trialRest{right_ind},trialsDx.timeRest{right_ind}]=extractTrial(data,r);
    [trialsDx.trial{right_ind},trialsDx.time{right_ind}]=extractTrial(data,d);
    right_ind=right_ind+1;
end
    


end


function [trial,time]=extractTrial(data,indx)

timestep=1/data.fsample;
beg_sample=data.events(indx).latency;
end_sample=beg_sample+data.events(indx).duration-1;        

trial=data.data(:,beg_sample:end_sample);
time=beg_sample/data.fsample:timestep:end_sample/data.fsample;
